<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="strutsTags"%>

<table id="employeeSelector" style="display: none; width: 100%">
	<tr>
		<td>
			<s:hidden name="emp" />
			<s:hidden name="attdDateFrom" />
			<s:hidden name="attdDateTo" />
			<s:hidden name="dept" />
			<s:hidden name="machineNo" />
			<s:hidden name="status" />
			<s:hidden name="empIdStr" />
			<input type="hidden" id="empStatus" value="0">
			<s:empselector id="empselector1" name="emp.empDeptNo.departmentName"
				condition="empNeedCard&empStatus" hiddenFieldName="emp.empDeptNo.id" />
		</td>
	</tr>
</table>

<div id="addCard" style="display: none">
	<!-- 添加补卡信息 -->
	<table border="0" cellspacing="2" width="100%">
		<tr>
			<td>
				刷卡时间:
				<s:textfield id="attdCardTime" name="attdCardTime" required="true"
					size="17" maxlength="16" onchange="showAttdDate(this.value);" />
				<img
					onclick="WdatePicker({el:'attdCardTime',dateFmt:'yyyy-MM-dd HH:mm'})"
					class="dateimg"
					src="resource/js/My97DatePicker/skin/datePicker.gif">
				&nbsp;
			</td>
			<td>
				备注:
			</td>
			<td rowspan="2" valign="middle">
				<s:textarea id="memo" name="memo" cols="28" rows="2" value="忘记刷卡"
					onkeyup="if(this.value.length>255){this.value=this.value.substring(0, 255);}"></s:textarea>
			</td>
		</tr>
		<tr>
			<td>
				<input type="checkbox" class="checkbox" id="addNightShift"
					name="addNightShift" onclick="changeAddNightShift();"
					valign="bottom" />
				隔夜刷卡 考勤日期:
				<input id="attdDate" name="attdDate" size="10" readonly="readonly"
					class="nothinginput" />
			</td>
		</tr>
		<tr>
			<td colspan="4" class="prompt_div_bottom" align="center">
				<input class="button" type="button" name="submit_button"
					onclick="saveData();" value="确定" id="btnSave">
				<input class="button" type="button" name="clear_button"
					onclick="HRMCommon.closeDialog('dlgEmpListDiv');" value="取消">
			</td>
		</tr>
	</table>
</div>
<div id="syncToMachine" style="display: none">
	<!-- 向考勤机中同步员工信息 -->
	<table border="0" cellspacing="2" width="100%">
		<tr>
			<td>
				考勤机号:
			</td>
			<td colspan="3">
				<s:iterator value="machineList" id="toMachine">
					<input type="checkbox" class="checkbox"
						name="syncToMachine_machineNo" value="<s:property value="macId"/>" />
					<s:property value="macNo" />
				</s:iterator>
			</td>
		</tr>
		<tr>
			<td>
				同步用户类型:
			</td>
			<td colspan="3">
				<input type="radio" class="radio" name="userType" value="0"
					checked="checked" />
				普通用户
				<input type="radio" class="radio" name="userType" value="1" />
				考勤机管理员
			</td>
		</tr>
		<tr>
			<td colspan="4">
				<font color="red">备注：更新用户同步记录时，如果考勤机中没有用户，自动添加；如果考勤机中有用户，自动更新。</font>
			</td>
		</tr>
		<tr>
			<td colspan="4" class="prompt_div_bottom" align="center">
				<input class="button" type="button" name="btn"
					onclick="syncToMachine();" value="确定" id="btnSyncToMachine">
				<input class="button" type="button" name="btn"
					onclick="HRMCommon.closeDialog('dlgEmpListDiv');" value="取消">
			</td>
		</tr>
	</table>
</div>
<div id="batchRead" style="display: none">
	<!-- 批量读取考勤机中刷卡记录 -->
	<table border="0" cellspacing="2">
		<tr>
			<td>
				刷卡开始时间:
			</td>
			<td>
				<s:textfield id="syncStartTime" name="syncStartTime" required="true"
					size="17" maxlength="16" onchange="showAttdDate(this.value);" />
				<img
					onclick="WdatePicker({el:'syncStartTime',dateFmt:'yyyy-MM-dd HH:mm'})"
					class="dateimg"
					src="resource/js/My97DatePicker/skin/datePicker.gif">
			</td>
		</tr>
		<tr>
			<td>
				刷卡结束时间:
			</td>
			<td>
				<s:textfield id="syncEndTime" name="syncEndTime" required="true"
					size="17" maxlength="16" onchange="showAttdDate(this.value);" />
				<img
					onclick="WdatePicker({el:'syncEndTime',dateFmt:'yyyy-MM-dd HH:mm'})"
					class="dateimg"
					src="resource/js/My97DatePicker/skin/datePicker.gif">
			</td>
		</tr>
		<tr>
			<td>
				考勤机号:
			</td>
			<td>
				<s:iterator value="machineList">
					<input type="checkbox" class="checkbox" name="batchRead_machineNo"
						value="<s:property value="macId"/>" />
					<s:property value="macNo" />
				</s:iterator>
			</td>
		</tr>
		<tr>
			<td colspan="4" class="prompt_div_bottom">
				<input class="button" type="button" name="btn"
					onclick="batchRead();" value="确定">
				<input class="button" type="button" name="btn"
					onclick="HRMCommon.closeDialog('dlgEmpListDiv');" value="取消">
			</td>
		</tr>
	</table>
</div>
<div id="syncToSystem" style="display: none">
	<!-- 批量读取考勤机中刷卡记录 -->
	<table border="0" cellspacing="2" width="100%">
		<tr>
			<td>
				考勤机号:
			</td>
			<td>
				<s:iterator value="machineList">
					<input type="checkbox" class="checkbox"
						name="syncToSystem_machineNo" value="<s:property value="macId"/>" />
					<s:property value="macNo" />
				</s:iterator>
			</td>
		</tr>
		<tr>
			<td>
				考勤机无效用户:
			</td>
			<td colspan="3">
				<input type="radio" class="radio" name="isClean" value="0"
					checked="checked" />
				自动清理
				<input type="radio" class="radio" name="isClean" value="1" />
				设为不同步
			</td>
		</tr>
		<tr>
			<td colspan="4">
				<font color="red">备注：如果考勤机数据与同步记录不一致，勾选“自动清理”，自动执行“删除同步记录”操作。</font>
			</td>
		</tr>
		<tr>
			<td colspan="4" class="prompt_div_bottom" align="center">
				<input class="button" type="button" name="btn"
					onclick="syncToSystem();" value="确定">
				<input class="button" type="button" name="btn"
					onclick="HRMCommon.closeDialog('dlgEmpListDiv');" value="取消">
			</td>
		</tr>
	</table>
</div>

<div id="dlgSyncSingleUser" class="prompt_div_inline">
	<input type="hidden" id="singleEmpId" name="singleEmpId" />
	<input type="hidden" id="singleMacId" name="singleMacId" />
	<!-- 单个同步考勤机用户，可以修改权限和密码 -->
	<table width="100%">
		<tr>
			<td>
				登记号码:
			</td>
			<td id="td_empManchineNo">
			</td>
		</tr>
		<tr>
			<td>
				考勤卡号:
			</td>
			<td id="td_empCardNo">
			</td>
		</tr>
		<tr>
			<td>
				同步用户类型:
			</td>
			<td>
				<input type="radio" class="radio" id="singleuserType0"
					name="singleuserType" value="0" checked="checked" />
				普通用户
				<input type="radio" class="radio" id="singleuserType1"
					name="singleuserType" value="1" />
				考勤机管理员
			</td>
		</tr>
		<tr>
			<td>
				考勤机密码:
			</td>
			<td>
				<input type="text" id="singleUserPass" name="singleUserPass"
					value="" />
			</td>
		</tr>
		<tr>
			<td colspan="2" class="prompt_div_bottom">
				<input class="button" type="button" name="btn"
					onclick="sysToMachine();" value="确定">
				<input class="button" type="button" name="btn"
					onclick="HRMCommon.closeDialog('dlgSyncSingleUser');" value="取消">
			</td>
		</tr>
	</table>
</div>

<div id="dlgCopyFinger" style="display: none">
	<input type="hidden" id="copyfinger_asrIds" />
	<!-- 指纹拷贝 -->
	<table border="0" cellspacing="2" width="100%">
		<tr>
			<td width="30%">
				从考勤机:
			</td>
			<td align="left" id="TD_fromManchine">
			</td>
		</tr>
		<tr>
			<td width="30%">
				到考勤机:
			</td>
			<td align="left">
				<s:iterator value="machineList">
					<s:if test="macHasFinger == 1">
						<input type="checkbox" class="checkbox"
							name="copyfinger_machineNo" value="<s:property value="macId"/>" />
						<s:property value="macNo" />
					</s:if>
				</s:iterator>
			</td>
		</tr>
		<tr>
			<td colspan="4">
				&nbsp;
			</td>
		</tr>
		<tr>
			<td colspan="4" class="prompt_div_bottom" align="center">
				<input class="button" type="button" name="btn"
					onclick="copyToMachine();" value="确定">
				<input class="button" type="button" name="btn"
					onclick="HRMCommon.closeDialog('dlgCopyFinger');" value="取消">
			</td>
		</tr>
	</table>
</div>

<div id="dlgSyncResultDiv" title="同步结果"
	style="display: none; height: 480"></div>
<script type="text/javascript" language="javascript">
// 当刷卡时间改变时修改考勤日期；
function showAttdDate(cardTime){
	changeAddNightShift();
}

// 根据选择的类型确定考勤日期；
function changeAddNightShift(){
	var cardTime = document.getElementById("attdCardTime").value;
	if(cardTime == null || cardTime.length==0) return;
	var cardDate = HRMCommon.convertStrToDate(cardTime.substring(0,10));
	
	var checkObj = document.getElementById("addNightShift");
	if(checkObj.checked == false){// 非隔夜刷卡，考勤日期为刷卡所在日期；
		document.getElementById("attdDate").value = cardTime.substring(0,10);
	    document.getElementById("memo").value = "忘记刷卡";
    }else{// 隔夜刷卡，考勤日期为刷卡所在日期前一天；
        var timeDayArr = cardTime.substring(0,10).split('-'); 
        var newDate = new Date(timeDayArr[0],timeDayArr[1]-1,timeDayArr[2]-1);
        
        document.getElementById("attdDate").value = newDate.toHRMDateString();
        document.getElementById("memo").value = "夜班忘记刷卡";
    }
}

//同考勤机同步数据操作
function syncData(option){
	if(option=="addCard"){
		document.getElementById('ui-dialog-title-dlgEmpListDiv').innerHTML="人工补卡";
		document.getElementById('empStatus').value=0;
	}
	if(option=="syncToMachine"){
		document.getElementById('ui-dialog-title-dlgEmpListDiv').innerHTML="更新同步记录";
		document.getElementById('empStatus').value=0;
	}
	if(option=="batchRead"){
		document.getElementById('ui-dialog-title-dlgEmpListDiv').innerHTML="同步刷卡记录";
		document.getElementById('employeeSelector').style.display = "none";
		document.getElementById("syncStartTime").value = "";
		document.getElementById("syncEndTime").value = "";
	}
	if(option=="syncToSystem"){
		document.getElementById('ui-dialog-title-dlgEmpListDiv').innerHTML="同步记录检查";
		document.getElementById('employeeSelector').style.display = "none";
	}
}

//切换弹出窗口操作
function switchOperate(operate){
	document.getElementById('employeeSelector').style.display = "";
	document.getElementById("addCard").style.display="none";
	document.getElementById("syncToMachine").style.display="none";
	document.getElementById("batchRead").style.display="none";
	document.getElementById("syncToSystem").style.display="none";
	document.getElementById(operate).style.display="";
	syncData(operate);
}

//保存提交操作(点击Dialog'保存按钮')
function saveData(){
    var empStrId = empSelector_choseEmpIdStr();
    if(empStrId == ''){
        alert("请至少选择一个员工！");
        return false;
    }
    //日期正则表达式
    var dateReg = /^\d{4}-\d{1,2}-\d{1,2}$/;
    var datetimeReg = /^\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}(:\d{1,2})?$/;
    if(!dateReg.test(document.getElementById('attdDate').value)){
        alert("考勤日期格式错误！");
        return false;
    }
    if(!datetimeReg.test(document.getElementById('attdCardTime').value)){
        alert("刷卡时间格式错误！");
        return false;
    }
    if(document.getElementById('memo').value == ''){
        alert("请填写备注！");
        return false;
    }
    if(document.getElementById('attdCardTime').value.split(':').length ==2){
        document.getElementById('attdCardTime').value = document.getElementById('attdCardTime').value +":00";
    }
    var examinDateArr = document.getElementById('attdDate').value.split('-');
    var examinDate = new Date(examinDateArr[0],examinDateArr[1]-1,examinDateArr[2]);
    var cardDateArr = document.getElementById('attdCardTime').value.split(' ')[0].split('-');
    var cardDate = new Date(cardDateArr[0],cardDateArr[1]-1,cardDateArr[2]);
    if(examinDate.getTime() != cardDate.getTime()){
        if(cardDate.getTime()-examinDate.getTime()!=86400000){
            alert("刷卡时间和考勤日期不一致！");
            return false;
        }
        /*if(!window.confirm('刷卡时间与考勤日期不是同一天，请确认是否隔夜班次？')){
            return false;
        }*/
    }
    
    document.getElementById('btnSave').disabled='disabled';
    document.getElementById('empIdStr').value = empStrId;
    DwrForAttend.addCardData(document.getElementById('empIdStr').value,document.getElementById('attdDate').value,
    	    document.getElementById('attdCardTime').value,document.getElementById('memo').value,'<s:property value="#session['empName']"/>',callback);
    function callback(data){
    	HRMCommon.actionMsgHandler(data);
    	HRMCommon.closeDialog('dlgEmpListDiv');
        document.forms[0].submit();
    }
}

//向考勤机中同步员工信息
//同步提交操作(点击Dialog'同步按钮')
function syncToMachine(){
	document.getElementById('btnSyncToMachine').disabled='disabled';
	document.getElementById('empIdStr').value = empSelector_choseEmpIdStr();
    if(document.getElementById('empIdStr').value==""||document.getElementById('empIdStr').value.length<=0){
		alert("请选择您要同步的员工！");
		document.getElementById('btnSyncToMachine').disabled='';
		return false;
    }
    var machineNo = "";
    var machineArray = document.getElementsByName("syncToMachine_machineNo");
    if(machineArray == null){
		alert("请先添加考勤机！");
		document.getElementById('btnSyncToMachine').disabled='';
		return false;
    }
    for(var i=0;i<machineArray.length;i++){
		if(machineArray[i].checked){
			if(machineNo != ""){
				machineNo += ";";
			}
			machineNo += machineArray[i].value;
		}
    }
    if(machineNo == ""){
		alert("请选择考勤机！");
    	document.getElementById('btnSyncToMachine').disabled='';
		return false;
    }
    var radio = document.getElementsByName('userType');
    var userType;
    for(var i=0;i<radio.length;i++){
        if(radio[i].checked){
        	userType = radio[i].value;
        }
    }

    HRMCommon.closeDialog('dlgEmpListDiv');
    $('#divWait').show();
	DwrSyncAttdMachine.syncToMachine(document.getElementById('empIdStr').value,machineNo,userType, null, callback);
    function callback(data){
    	$('#divWait').hide();
    	showReturnInfo(data);
    }
}

//同步考勤机中员工信息
//同步提交操作(点击Dialog'同步按钮')
function syncToSystem(){
    var machineNo = "";
    var machineArray = document.getElementsByName("syncToSystem_machineNo");
    if(machineArray == null){
		alert("请先添加考勤机！");
		return false;
    }
    for(var i=0;i<machineArray.length;i++){
		if(machineArray[i].checked){
			if(machineNo != ""){
				machineNo += ";";
			}
			machineNo += machineArray[i].value;
		}
    }
    var isClean;
    var radio = document.getElementsByName("isClean");
    for(var i=0;i<radio.length;i++){
		if(radio[i].checked){
			isClean = radio[i].value;
		}
    }
    if(machineNo == ""){
		alert("请选择考勤机！");
		return false;
    }

    HRMCommon.closeDialog('dlgEmpListDiv');
    $('#divWait').show();
    DwrSyncAttdMachine.syncToSystem(machineNo,isClean,callback);
    function callback(data){
    	$('#divWait').hide();
    	showReturnInfo(data);
    }
}

//批量读取考勤机中员工刷卡记录
//读取提交操作(点击Dialog'读取按钮')
function batchRead(){
    var machineNo = "";
    var machineArray = document.getElementsByName("batchRead_machineNo");
    if(machineArray == null){
		alert("请先添加考勤机！");
		return false;
    }
    for(var i=0;i<machineArray.length;i++){
		if(machineArray[i].checked){
			if(machineNo != ""){
				machineNo += ";";
			}
			machineNo += machineArray[i].value;
		}
    }
    if(machineNo == ""){
		alert("请选择考勤机！");
		return false;
    }

    var syncStartTime = document.getElementById("syncStartTime").value.trim();
    var syncEndTime   = document.getElementById("syncEndTime").value.trim();
    var datetimeReg = /^\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}(:\d{1,2})?$/;
    if(syncStartTime!='' && !datetimeReg.test(syncStartTime)){
        alert("开始日期格式错误！");
        return;
    }
    if(syncEndTime!='' && !datetimeReg.test(syncEndTime)){
        alert("结束日期格式错误！");
        return;
    }
    if(syncStartTime!='' && syncEndTime!='' && syncStartTime > syncEndTime){
        alert("开始时间大于结束时间，请重新选择！");
        return;
    }
    
	HRMCommon.closeDialog('dlgEmpListDiv');
	$('#divWait').show();
    DwrSyncAttdMachine.batchRead(machineNo, syncStartTime, syncEndTime, callback);
    function callback(data){
    	$('#divWait').hide();
    	showReturnInfo(data);
    }
}
//批量删除考勤机中员工信息
//删除提交操作(点击Dialog'删除按钮')
function batchDelete(){
    var recordIds = "";
    var checkBoxName = document.getElementsByName('changIds');   
	var applierIds = "";
	for(var i=0;i<checkBoxName.length;i++){
		if(checkBoxName[i].checked===true){
			if(recordIds != "") recordIds += ";";
			recordIds += checkBoxName[i].value;
		}
	}
	if (recordIds == ""){
		alert("请至少选择一条记录！");
		return;
	}
    if(!confirm("您确定要删除同步记录，同时删除考勤机中的用户数据吗？")){
		return;
	}

    $('#divWait').show();
    DwrSyncAttdMachine.batchDelete(recordIds,callback);
    function callback(data){
    	$('#divWait').hide();
    	showReturnInfo(data);
    }
}
function empSelecotr(operate){
	switchOperate(operate);
	empSelector_clearLeftEmp();
	empSelector_clearRightEmp();
	empSelector_searchEmp();
	HRMCommon.openDialog('dlgEmpListDiv');
}

// 考勤机操作返回信息统一显示函数；
function showReturnInfo(info){
	var closeBtn = "<div class='prompt_div_bottom'><input value='关闭' class=\"button\" type=\"button\" onclick='hrm.common.closeDialog(\"dlgSyncResultDiv\");document.forms[0].submit();'/></div>";
	var resultField = "<textarea rows='10' cols='70' readonly='readonly'>"+info+"</textarea>";
	$('#dlgSyncResultDiv').html(resultField+closeBtn);
	hrm.common.openDialog('dlgSyncResultDiv');
}

HRMCommon.initDialog('dlgSyncResultDiv',480);
HRMCommon.initDialog('dlgEmpListDiv',560);
</script>
